/*
 * The MIT License
 *
 * Copyright 2015 chronosinfo.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions?
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * Autor John Vanderson M Lima
 */
package com.chronos.pafecf.modelo;

import com.chronos.pafecf.entidades.EcfImpressora;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author usuario
 */
public class ImpressoraDao extends GenericDao {

    private String sql;

    public EcfImpressora getImpressora(int id) throws SQLException {
        EcfImpressora imp = null;
        ResultSet rs = execultQuery("select * from ecf_impressora where id=?", id);
        if (rs.next()) {
            imp = popularImpressora(rs);
        }
        rs.close();
        return imp;
    }

    public EcfImpressora getImpressoraMarca(String marca) throws SQLException {
        EcfImpressora imp = null;
        ResultSet rs = execultQuery("select * from ecf_impressora where marca=?", marca);
        if (rs.next()) {
            imp = popularImpressora(rs);
        }
        rs.close();
        return imp;
    }

    private EcfImpressora popularImpressora(ResultSet rs) throws SQLException {
        EcfImpressora impressora = new EcfImpressora();
        impressora.setId(rs.getInt("ID"));
        impressora.setNumero(rs.getInt("NUMERO"));
        impressora.setCodigo(rs.getString("CODIGO"));
        impressora.setSerie(rs.getString("SERIE"));
        impressora.setIdentificacao(rs.getString("IDENTIFICACAO"));
        impressora.setMc(rs.getString("MC"));
        impressora.setMd(rs.getString("MD"));
        impressora.setVr(rs.getString("VR"));
        impressora.setTipo(rs.getString("TIPO"));
        impressora.setMarca(rs.getString("MARCA"));
        impressora.setModelo(rs.getString("MODELO"));
        impressora.setModeloAcbr(rs.getString("MODELO_ACBR"));
        impressora.setModeloDocumentoFiscal(rs.getString("MODELO_DOCUMENTO_FISCAL"));
        impressora.setVersao(rs.getString("VERSAO"));
        impressora.setLe(rs.getString("LE"));
        impressora.setLef(rs.getString("LEF"));
        impressora.setMfd(rs.getString("MFD"));
        impressora.setLacreNaMfd(rs.getString("LACRE_NA_MFD"));
        impressora.setDocto(rs.getString("DOCTO"));
        impressora.setNumeroEcf(rs.getString("ECF_IMPRESSORA"));
        impressora.setDataInstalacaoSb(rs.getDate("DATA_INSTALACAO_SB"));
        impressora.setHoraInstalacaoSb(rs.getString("HORA_INSTALACAO_SB"));
        return impressora;
    }

    public List<EcfImpressora> getAllImpressora() throws SQLException {
        List<EcfImpressora> toReturn = new LinkedList<>();
        ResultSet rs = execultQuery("select * from ecf_impressora");
        while (rs.next()) {
            toReturn.add(popularImpressora(rs));
        }
        rs.close();
        return toReturn;
    }

    public int totalRegistro() throws SQLException {
        int total;
        ResultSet rs = execultQuery("select count(*) as total from ecf_impressora");
        total = rs.getInt("total");
        rs.close();
        return total;
    }

    public boolean consultaIdImpressora(int id) throws SQLException {
        ResultSet rs = execultQuery("select * from ecf_impressora where id=?", id);
        if (rs.next()) {
            return true;
        }

        return false;
    }

    public EcfImpressora insertImpressora(EcfImpressora imp) throws SQLException {
        sql = "insert into ecf_impressora (id,"
                + " numero,"
                + " codigo,"
                + " serie,"
                + " identificacao,"
                + " mc,"
                + " md,"
                + " vr,"
                + " tipo,"
                + " marca,"
                + " modelo,"
                + " modelo_acbr,"
                + " modelo_documento_fiscal,"
                + " versao,"
                + " le,"
                + " lef,"
                + " mfd,"
                + " lacre_na_mfd,"
                + " docto,"
                + " data_instalacao_sb,"
                + " hora_instalacao_sb)"
                + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?";
        execultCommand(sql, imp.getId(), imp.getNumero(), imp.getSerie(), imp.getIdentificacao(), imp.getMc(),
                imp.getMd(), imp.getVr(), imp.getTipo(), imp.getMarca(), imp.getModelo(), imp.getModeloAcbr(),
                imp.getModeloDocumentoFiscal(), imp.getVersao(), imp.getLe(), imp.getLef(), imp.getMfd(),
                imp.getDocto(), imp.getDataInstalacaoSb(), imp.getHoraInstalacaoSb());
        return imp;
    }

    public EcfImpressora updateImpressora(EcfImpressora imp) throws SQLException {
        sql = "update ecf_impressora\n"
                + "set numero = ?,"
                + "codigo = ?,"
                + "serie = ?,"
                + "identificacao = ?,"
                + "mc = ?"
                + "md = ?,"
                + "vr = ?,"
                + "tipo = ?,"
                + "marca = ?,"
                + "modelo = ?,"
                + "modelo_acbr = ?,"
                + "modelo_documento_fiscal = ?,"
                + "versao = ?,"
                + "le = ?,"
                + "lef = ?,"
                + "mfd = ?,"
                + "lacre_na_mfd = ?,"
                + "docto = ?,"
                + "data_instalacao_sb = ?,"
                + "hora_instalacao_sb = ?,"
                + "ecf_impressora = ?,"
                + "where (id = ?)";
        execultCommand(sql, imp.getNumero(), imp.getSerie(), imp.getIdentificacao(), imp.getMc(),
                imp.getMd(), imp.getVr(), imp.getTipo(), imp.getMarca(), imp.getModelo(), imp.getModeloAcbr(),
                imp.getModeloDocumentoFiscal(), imp.getVersao(), imp.getLe(), imp.getLef(), imp.getMfd(),
                imp.getDocto(), imp.getDataInstalacaoSb(), imp.getHoraInstalacaoSb(),imp.getId());
        return imp;
    }
}
